﻿/*
 Copyright (c) 2011 Seth Juarez

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

using System;
using System.IO;
using ml.Attributes;
using ml.Tests.Model;
using System.Xml.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ml.Tests
{
    [TestClass]
    public class SerializationTests
    {
        [TestMethod]
        public void Serialize_Generic_Property()
        {
            var stream = GetSerialized_Generic_Property();
            Assert.IsNotNull(stream, "Serialization Failed");
        }

        [TestMethod]
        public void Deserialize_Generic_Property()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Property));

            var stream = GetSerialized_Generic_Property();
            stream.Position = 0;

            Property p = (Property)serializer.Deserialize(stream);

            Assert.IsNotNull(p, "Deserialization Failed");
        }

        private static Stream GetSerialized_Generic_Property() {
            Property p = new Property {
                Name = "Count",
                Type = typeof(int),
                isMethod = false
            };

            //Create our own namespaces for the output
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

            //Add an empty namespace and empty value
            ns.Add("", "");

            XmlSerializer serializer = new XmlSerializer(p.GetType());

            var stream = new MemoryStream();
            serializer.Serialize(stream, p, ns);
            stream.Position = 0;
            return stream;
        }

        [TestMethod]
        public void Serialize_String_Property()
        {
            var stream = GetSerialize_String_Property();
            Assert.IsNotNull(stream, "Serialization Failed");
        }

        [TestMethod]
        public void Deserialize_String_Property()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(StringProperty));
            var stream = GetSerialize_String_Property();
            stream.Position = 0;

            StringProperty p = (StringProperty)serializer.Deserialize(stream);
            Assert.IsNotNull(p, "Deserialization Failed");
        }

        private static Stream GetSerialize_String_Property() {
            var data = Student.GetData();
            var typeDesc = Converter.GetDescription(typeof(Student))
                                        .BuildDictionaries<Student>(data);

            StringProperty p = typeDesc.Features[5] as StringProperty;

            //Create our own namespaces for the output
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

            //Add an empty namespace and empty value
            ns.Add("", "");

            XmlSerializer serializer = new XmlSerializer(p.GetType());

            var stream = new MemoryStream();
            serializer.Serialize(stream, p, ns);
            stream.Position = 0;
            return stream;
        }

        [TestMethod]
        public void Serialize_Type_Description()
        {
            var stream = GetSerialize_Type_Description();
            Assert.IsNotNull(stream, "Serialization Failed");
        }

        [TestMethod]
        public void Deserialize_Type_Description()
        {
            var stream = GetSerialize_Type_Description();
            stream.Position = 0;

            XmlSerializer serializer = new XmlSerializer(typeof(TypeDescription));

            TypeDescription t = (TypeDescription)serializer.Deserialize(stream);

            Assert.IsNotNull(t, "Deserialization Failed");
        }

        private static Stream GetSerialize_Type_Description() 
        {
            var data = Student.GetData();
            var typeDesc = Converter.GetDescription(typeof(Student))
                                        .BuildDictionaries<Student>(data);

            string path = Directory.GetCurrentDirectory() + @"\serialize_type_description_test.xml";

            //Create our own namespaces for the output
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

            //Add an empty namespace and empty value
            ns.Add("", "");

            XmlSerializer serializer = new XmlSerializer(typeDesc.GetType());
            var stream = new MemoryStream();
            serializer.Serialize(stream, typeDesc, ns);
            stream.Position = 0;
            return stream;
        }

    }
}
